package com.dreamana.components
{
	import com.dreamana.ui.BaseComponent;
	import com.dreamana.ui.SkinGraphics;
	
	import flash.display.BlendMode;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	
	
	public class ListCell extends BaseComponent
	{
		public static var defaultFormat:TextFormat = new TextFormat("Arial", 12, 0x0, null, null, null, null, null, TextFormatAlign.LEFT);
		
		protected var _selected:Boolean;
		
		protected var _backColor:int;
		protected var _label:DisplayObject;
		
		//colors for each state
		protected var _color:uint = 0xffffff;
		protected var _hoverColor:uint = 0xc0c0c0;
		protected var _activeColor:uint = 0x404040;
		protected var _selectedColor:uint = 0x000000;
		
		
		public function ListCell()
		{
		}
		
		/**
		 * Interface
		 */
		public function reset(w:int, h:int, text:String=""):void
		{
			_width = w;
			_height = h;
			
			//addChildren()
			if(!_label) {
				_label = new Label(w, defaultFormat.size +8, defaultFormat);//default size
				_label.blendMode = BlendMode.INVERT;
				this.addChild(_label);
			}
			if(_label is Label) {
				Label(_label).text = text;
				_label.y = h - _label.height >> 1;
			}
			
			//events & draw();
			this.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			this.addEventListener(MouseEvent.MOUSE_UP, onUp);
			this.selected = false;
		}
		
		override public function draw():void
		{
			var g:Graphics = this.graphics;
			g.clear();
			SkinGraphics.drawBlank(g, _width, _height, _backColor);
		}
		
		/**
		 * Interface
		 */
		override public function setStyle(style:String, value:Object):void
		{
			super.setStyle(style, value);
			
			switch(style) {
				case "color":
					_color = value as Number;
					invalidate();
					break;
				case "hoverColor":
					_hoverColor = value as Number;
					invalidate();
					break;
				case "activeColor":
					_activeColor = value as Number;
					invalidate();
					break;
				case "selectedColor":
					_selectedColor = value as Number;
					invalidate();
					break;
			}
		}
		
		//--- EVENT HANDLERS ---
		
		protected function onOver(event:MouseEvent):void
		{
			_backColor = _hoverColor;
			this.draw();
		}
		
		protected function onOut(event:MouseEvent):void
		{
			_backColor = _color;
			this.draw();
		}
		
		protected function onDown(event:MouseEvent):void
		{
			if(stage) stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
			
			_backColor = _activeColor;
			this.draw();
		}
		
		protected function onUp(event:MouseEvent):void
		{
			if(stage) stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
			
			_backColor = (_selected) ? _selectedColor : _color;
			this.draw();
		}
		
		//--- GETTER/SETTERS ---
		public function get label():DisplayObject { return _label; }
		public function set label(value:DisplayObject):void
		{
			if(_label && this.contains(_label)) this.removeChild(_label);
			
			_label = value;
			this.addChild(_label);
		}
		
		/**
		 * Interface
		 */
		public function get selected():Boolean { return _selected; }
		public function set selected(value:Boolean):void {
			_selected = value;
			
			if(_selected) {
				this.removeEventListener(MouseEvent.ROLL_OVER, onOver);
				this.removeEventListener(MouseEvent.ROLL_OUT, onOut);
				
				_backColor = _selectedColor;
				this.draw();
			}
			else {
				this.addEventListener(MouseEvent.ROLL_OVER, onOver);
				this.addEventListener(MouseEvent.ROLL_OUT, onOut);
				
				_backColor = _color;
				this.draw();
			}
		}
	}
}